<?php
class Merchant_Model_WarehouseProducts
{

	private $_table;
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_WarehouseProducts();
	}


	public function add($row){
		return $this->_table->insert($row);
	}

	public  function update($row,$value,$field='wp_id'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);
		return $this->_table->update($row, $where);

	}


	public function delete($value,$field='wp_id'){

		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?", $value);

		return $this->_table->delete($where);

	}


	public function getByValue($value,$field='wp_id'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}

    public function getByCondition($condition,$type='*',$orderby="",$page=0,$pageSize=0){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition['product_id']) && $condition['product_id'] != ""){
			$select->where("product_id = ?",$condition['product_id']);
		}
		if(isset($condition['warehouse_id']) && $condition['warehouse_id'] != ""){
			$select->where("warehouse_id = ?",$condition['warehouse_id']);
		}
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchone($select);
        }else{
            if($orderby!='')
            {
                $orderbyArr= explode(',',$orderby);
                foreach($orderbyArr as $order)
                {
                    $select->order($order);
                }

            }
            if($pageSize>0 && $page>0)
            {

                $start = ($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
		    return $this->_table->getAdapter()->fetchAll($select);
        }
	}


     public function getByConditionJoin($condition,$type='*',$orderby="",$page=0,$pageSize=0){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
		if(isset($condition['summary']) && $type == "*"){
			$type = $type.",sum(wp_quantity) as total_quantity,sum(wp_quantity_hold) as total_quantity_hold";
		}else if(isset($condition['summary']) && $type != "*"){
			$type = "count(distinct warehouse_products.product_id)";
		}
        $select->from($table,$type);
        $select->join('product', "{$table}.product_id = product.product_id", '*');
        $select->where("1 =?",1);
        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"]!=''){
            $select->where("warehouse_id =?",$condition["warehouse_id"]);
        }

        if(isset($condition["product_title"]) && $condition["product_title"]!=''){
            $select->where(" product_title =?",$condition["product_title"]);
        }
        if(isset($condition["product_sku"]) && $condition["product_sku"]!=''){
            $select->where(" product_sku =? ",$condition["product_sku"]);
        }
        if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
            $select->where(" warehouse_products.customer_id = ? ",$condition["customer_id"]);
        }

        if(isset($condition["product_status"]) && $condition["product_status"]!=''){
            $select->where(" product_status = ?",$condition["product_status"]);
        }
		if(isset($condition['summary']) && $type == "*,sum(wp_quantity) as total_quantity,sum(wp_quantity_hold) as total_quantity_hold"){
			$select->group(array("warehouse_id","warehouse_products.product_id"));
		}
        if('count(distinct warehouse_products.product_id)'==$type || 'count(*)'==$type){
            return $this->_table->getAdapter()->fetchone($select);
        }else{
            if($orderby!=''){
                $orderbyArr= explode(',',$orderby);
                foreach($orderbyArr as $order){
                    $select->order($order);
                }

            }
            if($pageSize>0 && $page>0){
                $start = ($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
		    return $this->_table->getAdapter()->fetchAll($select);
        }

	}
	
	public function lockForUpdate($productId,$warehouseId){		 
		$sql = "select * from warehouse_products where product_id='$productId' and warehouse_id='$warehouseId' for update";
		return $this->_table->getAdapter()->fetchRow($sql);
	}
}